package com.example.controller;

import com.example.entity.Goods;
import com.example.service.GoodsService;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @GetMapping("/list")
    public String list(Model model) {
        List<Goods> goods = goodsService.getAllGoods();
        model.addAttribute("goods", goods);
        return "goods/list";
    }

    @GetMapping("/add")
    public String toAddPage() {
        return "goods/add";
    }

    @PostMapping("/add")
    public String add(Goods goods) {
        goodsService.addGoods(goods);
        return "redirect:list";
    }

    @GetMapping("/edit/{gid}")
    public String toEditPage(@PathVariable("gid") Integer gid, Model model) {
        Goods goods = goodsService.getGoodsById(gid);
        model.addAttribute("goods", goods);
        return "goods/edit";
    }

    @PostMapping("/edit")
    public String edit(Goods goods) {
        goodsService.updateGoods(goods);
        return "redirect:/goods/list";
    }

    @GetMapping("/delete/{gid}")
    public String delete(@PathVariable("gid") Integer gid) {
        goodsService.deleteGoods(gid);
        return "redirect:/goods/list";
    }

    //修改个人信息
}
